<?php

class Model_Review {

    public function insertReview($review, $imageList) {
        if (isset($review) && $review != null) {
            $content = $review['reviewContent'];                        
//            $query = sprintf("INSERT INTO `review`(`content`, `imageList`) 
//            VALUES (N'%s','%s')"
//                    , mysql_real_escape_string($content)
//                    , mysql_real_escape_string($imageList));
            $data = array(
                'content' => mysql_real_escape_string($content),
                'imageList' => mysql_real_escape_string($imageList),                
            );
            
            $db = Model_Connections::getInstance()->getDbConnect();     
            $numRows = $db->insert('review', $data);
                   
//            $data = $db->query($query);            
            $query = "Select Max(id) from review";
            $id = $db->fetchAll($query);
            if ($id != null)
                return $id[0]['Max(id)'];
            return null;
        }
    }

    private function processReview($reviewContent, $imageList) {
        $contents = explode("#", $reviewContent);
        $images = null;
        if ($imageList != "") {
            $images = explode(",", $imageList);
        }

        $contentSize = count($contents);
        $imageSize = 0;
        if ($images != null) {
            $imageSize = count($images);
        }        

        $hasMore = ($contentSize < $imageSize);
        $length = 0;
        if($hasMore){
            $length = $contentSize;
        }else{
            $length = $imageSize;
        }
        $data = array();
        for ($index = 0; $index < $length; $index++) {
            $item = array();
            $item['Content'] = $contents[$index];
            
            $item['Image'] = $images[$index];

            $data['Main'][$index] = $item;
        }

        if ($hasMore && $imageSize > 0) {
            $more = $imageSize - $contentSize + 1;
            $count = 0;
            for ($index = $more; $index < $imageSize; $index++) {
                $item = array();
                $item['Image'] = $images[$index];
                $data['More'][$count] = $item;

                $count += 1;
            }
        }

        return $data;
    }

    public function api_getReviewById($id) {
        if ($id != null) {
            $id = mysql_real_escape_string($id);

            $sql = "Select content,imageList from review where id = '" . $id . "'";


            $db = Model_Connections::getInstance()->getDbConnect();
            $data = $db->fetchAll($sql);
            if ($data != null)
                return $this->processReview($data[0]['content'], $data[0]['imageList']);
            else
                return null;
        }
        return null;
    }

    public function updateReviewById($id, $reviewContent, $imageLinks) {
        if (isset($id) && $id != null && isset($reviewContent) && $reviewContent != null && isset($imageLinks) && $imageLinks != null) {
            $id = mysql_real_escape_string($id);
            $reviewContent = mysql_real_escape_string($reviewContent);
            $imageLinks = mysql_real_escape_string($imageLinks);
            $sql = "UPDATE `review` SET `content` = N'" . $reviewContent . "', `imageList` = N'" . $imageLinks . "'  WHERE id = '" . $id . "'";
            $db = Model_Connections::getInstance()->getDbConnect();
            $data = $db->query($sql);
            return $data;
        }
        return null;
    }

    public function getReviewAllById($id) {
        $sql = "SELECT * FROM review where id = " . $id;
        $db = Model_Connections::getInstance()->getDbConnect();
        $data = $db->fetchAll($sql);
        return $data;
    }

    public function deleteReview($id) {
        if (isset($id) && $id != null) {
            $sql = sprintf("DELETE FROM `review` WHERE id = '%d' ", mysql_real_escape_string($id));
            $db = Model_Connections::getInstance()->getDbConnect();
            $data = $db->query($sql);
            return $data;
        }
    }

}

?>
